﻿#pragma once
#include "ZuiLib.h"
#include "LRefObject.h"

namespace ZuiLib {

class LBuilder :public LuaClass<ZBuilder>
{
public:
	void ctor(LuaFuncState& L)
	{
		pThis = new ZBuilder();
	}

	void dtor()
	{
		delete pThis;
	}

	int loadXmlFile(LuaFuncState& L)
	{
		pThis->LoadXml(L.arg(0).toString());
		return 0;
	}

	int loadXmlString(LuaFuncState& L)
	{
		pThis->LoadXml(L.arg(0).toString(), L.arg(0).objLen());
		return 0;
	}

	int create(LuaFuncState& L)
	{
		ZWindow* wnd = LuaClass<ZWindow>::luaToC(L.arg(0));
		if (wnd)
		{
			ZControl* ctrl = pThis->Create(wnd->GetManager());
			if(ctrl)
				return L.lreturn(LRefObject<ZControl>::cToLua(&L, ctrl, true));
			else
			{
				LOG(LS_ERROR) << "create control error:"<<pThis->GetErrorInfo();
				return 0;
			}
		}
		else
		{
			LOG(LS_ERROR) << "arg 0 needs window";
			return 0;
		}
	}

	BEGIN_MAP_FUNC(LBuilder)
	DECLARE_FUNC(loadXmlFile),
	DECLARE_FUNC(loadXmlString),
	DECLARE_FUNC(create),
	END_MAP_FUNC
};


}//namespace
